home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 430_01 / m68kdis / hex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-29  |  1.3 KB  |  69 lines

  1. /*
  2.  *                 Author:  Christopher G. Phillips
  3.  *              Copyright (C) 1994 All Rights Reserved
  4.  *
  5.  *                              NOTICE
  6.  *
  7.  * Permission to use, copy, modify, and distribute this software and
  8.  * its documentation for any purpose and without fee is hereby granted
  9.  * provided that the above copyright notice appear in all copies and
  10.  * that both the copyright notice and this permission notice appear in
  11.  * supporting documentation.
  12.  *
  13.  * The author makes no representations about the suitability of this
  14.  * software for any purpose.  This software is provided ``as is''
  15.  * without express or implied warranty.
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20.  
  21. char
  22. getachar(void)
  23. {
  24.     int    first = 1;
  25.     char    result = 0;
  26.     int    c;
  27.  
  28.     while ((c = getchar()) != EOF) {
  29.         if (c >= '0' && c <= '9')
  30.             if (first) {
  31.                 result = (c - '0') << 4;
  32.                 first = 0;
  33.             } else {
  34.                 result |= c - '0';
  35.                 return result;
  36.             }
  37.         if (c >= 'A' && c <= 'F')
  38.             if (first) {
  39.                 result = (c - 'A' + 10) << 4;
  40.                 first = 0;
  41.             } else {
  42.                 result |= c - 'A' + 10;
  43.                 return result;
  44.             }
  45.         if (c >= 'a' && c <= 'f')
  46.             if (first) {
  47.                 result = (c - 'a' + 10) << 4;
  48.                 first = 0;
  49.             } else {
  50.                 result |= c - 'a' + 10;
  51.                 return result;
  52.             }
  53.     }
  54.  
  55.     exit(0);
  56. }
  57.  
  58. int
  59. main(int argc, char **argv)
  60. {
  61.     char    c;
  62.  
  63.     while (1) {
  64.         c = getachar();
  65.         putchar(c);
  66.         fflush(stdout);
  67.     }
  68. }
  69.